﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Guide
{
    public partial class Printer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int EveryPageCount = 7;
            var listProvince = (List<Province>)Session["listprovince"];
            //var listProvince = (List<Province>)Session["listProvincePrint"];
            var listClusterID = listProvince.Select(o => o.ClusterID).Distinct().ToList();
            List<Province> clusterListProvince = new List<Province>();
            int i = 0;
            int j = 0;
            foreach (var item in listClusterID)
            {
                clusterListProvince = listProvince.Where(o => o.ClusterID == item).ToList();
                int count = clusterListProvince.Count;
                List<Province> listOrderBy = clusterListProvince.OrderBy(o => o.ProvinceCName).ToList();
                j = 0;
                int remainder = count - j * EveryPageCount;
                while (remainder > 0)
                {
                    var headerControl = (Header)LoadControl("Header.ascx");
                    headerControl.ID = "Header";
                    var footerControl = (Footer)LoadControl("Footer.ascx");
                    footerControl.ID = "Footer";
                    var control = (EveryPage)LoadControl("EveryPage.ascx");
                    control.EveryPageCount = EveryPageCount;
                    control.ID = item +"_"+i+"_EveryPage_" + j;
                    control.HeaderControl = headerControl;
                    control.FooterControl = footerControl;
                    if (remainder > EveryPageCount)
                    {
                        control.ListProvince = listOrderBy.GetRange(j * EveryPageCount,EveryPageCount);
                        //control.ListProvince = listOrderBy.GetRange(10, 15);
                    }
                    else
                    {
                        control.ListProvince = listOrderBy.GetRange(j* EveryPageCount, remainder);
                    }
                    content.Controls.Add(control);
                    j++;
                    remainder = count - j * EveryPageCount;
                }
                i++;
            }


            

            
            //while (remainder > 0)
            //{
            //    var control = (EveryPage)LoadControl("EveryPage.ascx");
            //    control.ID = _clusterName + "_EveryPage_" + j;
            //    if (remainder >= EveryPageCount)
            //    {
            //        control.ListProvince = listOrderBy.GetRange(j * EveryPageCount, (j + 1) * EveryPageCount);

            //    }
            //    else
            //    {
            //        control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, remainder);
            //    }
            //    this.Controls.Add(control);
            //    //catch (ArgumentException a)
            //    //{

            //    //    if (remainder > 0)
            //    //    {

            //    //    }
            //    //    this.Controls.Add(control);
            //    //    break;
            //    //}
            //    //i++;
            //}






















            //var listProvince = (List<Province>)Session["listprovince"];
            //var listClusterID = listProvince.Select(o => o.ClusterID).Distinct().ToList();

            //int i = 0;
            //foreach (var item in listClusterID)
            //{
            //    var control = (ClusterControl)LoadControl("ClusterControl.ascx");
            //    control.ID = item + "_" + i;
            //    control.ListProvince = listProvince.Where(o => o.ClusterID == item).ToList();
            //    control.EveryPageCount = 5;
            //    this.Controls.Add(control);
            //    i++;
            //}






            //while (true)
            //{
            //    var control = (EveryPage)LoadControl("EveryPage.ascx");
            //    control.ID = _clusterName + i;
            //    control.ListProvince = listOrderBy.GetRange(i * EveryPageCount, (i + 1) * EveryPageCount);
            //    this.Controls.Add(control);
            //}

            //foreach ()
            //{
            //    var control = (Stock)LoadControl("Every.ascx");
            //    control.ID = "Hello" + i;
            //    control.LabelContext = item;
            //    control.ListProvince = ((List<Province>)Session["listprovince"]).Where(o => o.ClusterID == item).ToList();
            //    this.Controls.Add(control);
            //    i = i + 1;
            //}
        }
    }
}